﻿using AutoMapper.Execution;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Primitives;
using NetTaste;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using SqlSugar.Extensions;
using System.Configuration;
using System.Data;
using System.Drawing.Printing;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using VM.Common.DataUntil;
using VM.Common.Encryption5Until;
using VM.Common.Request;
using VM.Common.Result;
using VM.DAL;
using VM.Entity.EntityDto;
using VM.Entity.EntityMap;
using VM.Entity.EntityMap.Member;
using VM.Entity.EntityMap.OAuth;
using VM.Entity.EntityMap.Token;
using VM.Entity.EntityMap.User;
using VM.IBLL.User;
using VM.IDAL;
using static Dm.net.buffer.ByteArrayBuffer;

namespace VM.BLL.User
{
    public class UserBLL : BaseBLL, IUserBLL
    {

        private readonly ISqlSuggarDAL _ISqlSuggarDAL;
        private readonly IConfiguration _IConfiguration;
        public UserBLL(ISqlSugarClient client, ISqlSuggarDAL sqlSuggarDAL, IConfiguration configuration)
        {
            _ISqlSuggarDAL = sqlSuggarDAL;
            _IConfiguration = configuration;
        }
         
 
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public ApiDataResult<LoginResDataEntity> loginAsync(UserLoginEntity obj) {
  
                string pattern = @"^[\p{L}\p{N}_\p{IsCJKUnifiedIdeographs}]+$";

                bool stsName = Regex.IsMatch(obj.username, pattern);
                bool stsPwd = Regex.IsMatch(obj.password, pattern);
                if (!(stsName && stsPwd))
                {
                    return new ApiDataResult<LoginResDataEntity>
                    {
                        code = 401,
                        msg = "输入用户名格式不正确",
                        data = null
                    };
                }
                try
                {
                    List<sys_user> checkUserList = _ISqlSuggarDAL.Query<sys_user>
                      (
                        e => e.name == obj.username || e.phone==obj.username
                    
                       ).ToList();
                    if (checkUserList.Count == 1)
                    {
                        List<sys_user> UserList = _ISqlSuggarDAL.Query<sys_user>
                      (
                        e => e.pwd == obj.password && (e.name == obj.username || e.phone == obj.username)
                       ).ToList();

                        //判断用户密码是否正确
                        if (UserList.Count == 1)
                        {
                            //如果正确，则进行统一鉴权登录
                            int UserId = (int)UserList[0].id;
                            //将UserId进行AES加密
                            string AESUserId = AESUntil.Encrypt(UserId.ToString());
                            LoginResDataEntity loginRes = new LoginResDataEntity();

                            loginRes.id = AESUserId;

                            return new ApiDataResult<LoginResDataEntity>
                            {
                                code = 200,
                                msg = "用户名密码正确",
                                data = loginRes

                            };
                        }
                        else
                        {
                            return new ApiDataResult<LoginResDataEntity>
                            {
                                code = 403,
                                msg = "用户名或密码错误",
                                data = null

                            };
                        }
                         
                      
                       
                    }
                else if (checkUserList.Count < 1)
                {

                    return new ApiDataResult<LoginResDataEntity>
                    {
                        code = 401,
                        msg = "当前用户不存在",
                        data = null

                    };

                }
                else
                {

                    return new ApiDataResult<LoginResDataEntity>
                    {
                        code = 401,
                        msg = "当前用户存在多条，不允许登录",
                        data = null

                    };
                }

            }
                catch (Exception e)
                {
                    return new ApiDataResult<LoginResDataEntity>
                    {
                        code = 401,
                        msg = "登录出错：" + e.Message,
                        data = null

                    };

                } 
            }
         
        public ApiResult logout()
        {
            ApiResult  res = new ApiResult()
            {
                code = 200,
                msg = "退出登录成功",
            };
            return res;
        }
       /// <summary>
      /// 获取用户信息
      /// </summary>
     /// <returns></returns>
        public ApiDataResult<UserInfoEntity> userinfo()
        {
            ApiDataResult<UserInfoEntity> res = new ApiDataResult<UserInfoEntity>()
            {

                code = 200,
                msg = "success",
               data=new UserInfoEntity()
               {
                   username = "admin",
                   avatar = "https://i.gtimg.cn/club/item/face/img/2/16022_100.gif",
                   roles = new[] { "Admin" },
                   permissions = new[] { 
                                        "read:system",
                                        "write:system",
                                        "delete:system" }
              }

        };
            return res;
        }
    }
}